import store from "@/store";
export const companyFirstSelect = (
  {
    field = "parentCompanyId",
    title = "单位",
    tableVisible = true,
    options = store.state.userCompanyList.reduce((acc, curr) => {
      if (!acc.find(item => item.parentCompanyId == curr.parentCompanyId)) {
        acc.push(curr);
      }
      return acc;
    }, []),
    col = { span: 4 },
    props = {
      clearable: true,
      filterable: true,
      multiple: false
    },
    validate,
    value = null
  },

  isAdmin
) => {
  let bbb = options
    .reduce((acc, curr) => {
      if (
        !acc.find(
          item =>
            item.parentCompanyId == curr.parentCompanyId ||
            !curr.parentCompanyId
        )
      ) {
        acc.push(curr);
      }
      return acc;
    }, [])
    .map(e => {
      return {
        value: e.parentCompanyId,
        label: e.companyName
      };
    });
  return {
    field,
    title,
    options: bbb,
    col,
    props,
    type: "select",
    hidden: isAdmin ? false : true,
    tableVisible: tableVisible && isAdmin ? tableVisible : false,
    filter: i => {
      return bbb.length ? bbb.find(v => v.value == i)?.label : i;
    },
    validate: validate,
    value: bbb?.find(i => i.value == Number(value))
      ? value
      : bbb.find(i => i.label == "全部")
      ? ""
      : bbb.length && !props.multiple
      ? bbb[0].value
      : null
  };
};
/**
 * @name 办公点
 */
export const companyFindSelect = (
  {
    field = "companyId",
    title = "单位",
    tableVisible = true,
    options = store.state.userCompanyList,
    col = { span: 4 },
    props = {
      clearable: true,
      filterable: true,
      multiple: false
    },
    parentCompanyId = "",
    validate,
    value = null
  },
  isAdmin
) => {
  let listOptions = options.filter(e => {
    return e.parentCompanyId == parentCompanyId || !e.parentCompanyId;
  });
  return {
    field,
    title,
    options: parentCompanyId ? listOptions : options,
    col,
    props,
    type: "select",
    hidden: isAdmin ? false : true,
    tableVisible: tableVisible && isAdmin ? tableVisible : false,
    filter: i => {
      return options.length ? options.find(v => v.value == i)?.label : i;
    },
    validate: validate,
    value: value ? value : options.length ? options[0].value : null
  };
};
export const companySelect = (
  {
    field = "companyId",
    title = "办公点",
    tableVisible = true,
    options = store.state.userCompanyList,
    col = { span: 4 },
    props = {
      clearable: true,
      filterable: true,
      multiple: false
    },
    validate,
    value = null
  },

  isAdmin
) => {
  return {
    field,
    title,
    options,
    col,
    props,
    type: "select",
    hidden: isAdmin ? false : true,
    tableVisible: tableVisible && isAdmin ? tableVisible : false,
    filter: i => {
      return options.length ? options.find(v => v.value == i)?.label : i;
    },
    validate: validate,
    value: options?.find(i => i.value == Number(value))
      ? value
      : options?.find(i => i.value == Number(store.state.companyId))
      ? Number(store.state.companyId)
      : options.length
      ? options[0].value
      : null
  };
};
export const companySelect1 = (
  {
    field = "companyId",
    title = "单位",
    options = store.state.userCompanyList,
    col = { span: 4 },
    props = {
      clearable: true,
      filterable: true
    },
    value = null
  },
  isAdmin
) => {
  return {
    field,
    title,
    options,
    col,
    props,
    type: "select",
    hidden: isAdmin ? false : true,
    tableVisible: isAdmin ? true : false,

    filter: i => {
      return store.state?.userCompanyList?.length
        ? store.state.userCompanyList.find(v => v.value == i)?.label
        : i;
    },
    value: value
  };
};

export const companyListSelect = (
  {
    field = "inPutcompanyIdList",
    title = "办公点",
    tableVisible = true,
    options = store.state.userCompanyList,
    col = { span: 4 },
    props = {
      clearable: true,
      filterable: true,
      "collapse-tags": true,
      multiple: true
    },
    validate,
    value = null
  },

  isAdmin
) => {
  return {
    field,
    title,
    options,
    col,
    props,
    type: "select",
    hidden: isAdmin ? false : true,
    tableVisible: tableVisible && isAdmin ? tableVisible : false,
    filter: i => {
      return options.length ? options.find(v => v.value == i)?.label : i;
    },
    validate: validate,
    value: options?.find(i => i.value == Number(value))
      ? value
      : options?.find(i => i.value == Number(store.state.companyId))
      ? Number(store.state.companyId)
      : null
  };
};
